/* Copyright (c) 2009 Byron Adams <byron.adams54@gmail.com>
   See the file LICENSE for licensing information. */

var translator = { 

  extension : Application.extensions.get("translator@dontfollowme.net"),
  
  /**
   * Returns an array of service objects that fit the criteria outlined by the
   * filter object. Services are sorted by meta rating and disabled services
   * are not returned.
   *
   * @example translator.getServices({type : "*"});
   * @result [{...}, {...}, {...}]
   *
   * @example translator.getServices({type : "translate", srcLang : "ENGLISH", destLang : "GERMAN"});
   * @result [{...}, {...}, {...}]
   *
   * @name translator.getServices()
   * @param Object filter
   */  
  getServices : function (filter) {
    var prefObj = Mozkit.json.decode(this.extension.prefs.get("services").value),
      plugin = filter.type == "*" ? {} : translator.plugins[filter.type],
      services = [];
    
    this.services.forEach(function (service) {
      var prefService = prefObj[service.meta.shortName];
      
      service.meta.rating = prefService.rating;
      service.meta.disabled = prefObj[service.meta.shortName].disabled;
      
      if (filter.type == "*" || ("serviceFilter" in plugin && plugin.serviceFilter(filter, service) && !service.meta.disabled))
        services.push(service);
    });
    
    return services.sort(function (a, b) {
      return a.meta.rating > b.meta.rating ? -1 : a.meta.rating < b.meta.rating ? 1 : 0;
    });
  },
  
  /**
   * Creates a service.
   *
   * @access public
   * @param Object service
   * @return void
   */  
  service : function (service) {
    var sPref = this.extension.prefs.get("services"), prefObj = Mozkit.json.decode(sPref.value);
    
    this.services = this.services || [];

    if (!(service.meta.shortName in prefObj))
      prefObj[service.meta.shortName] = {rating : 0, disabled : false};
    
    sPref.value = Mozkit.json.encode(prefObj);
    this.services.push(service);
    
    if ("init" in service) service.init.apply(this.services[ this.services.length-1 ], []);
    
  },
  
  plugin : function (name, plugin) {
    this.plugins = this.plugins || {};
    this.plugins[name] = plugin;

    this[name] = function () {
			return this.plugins[name].method.apply(this.plugins[name], arguments);
		};
  },

  /**
   * Returns a localized string from the translator 
   * string bundle.
   *
   * @access public
   * @param string
   * @return string
   */
  getLocale : function (prop) {
    if (undefined === this.locale) {
      this.locale = document.getElementById('translator-locales');
    }

    if (1 < arguments.length) {
      this.locale.getFormattedString.apply(this.locale, arguments);
    }

    return this.locale.getString(prop);
  },
  
  /**
   * Returns a language object representing the
   * users native language. will return false if the 
   * language is not supported.
   *
   * @access public
   * @return mixed
   */
  getNativeLanguage : function () {
    var lang = navigator.language, $lang = this.languages.find(lang);
    
    if (false === $lang) {
      $lang = this.languages.find(lang.split('-')[0]);
      if (false !== $lang) {
        this.languages.alias($lang.code, lang);  
      }  
    }
    
    return $lang;
  },
    
  getLanguage : function (query) {
    return this.languages.find(query);
  },
  
  debug : function () {
    if (this.extension.prefs.get("debug").value)
      for(var i = 0; i < arguments.length; i++)
        Application.console.log("[DEBUG] " + arguments[i]);
  }
};
